package com.jdls.tanhua.template;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teaopenapi.models.Config;
import com.jdls.tanhua.properties.SmsProperties;


public class SmsTemplate {
    //自动装配类
    private SmsProperties properties;
    public SmsTemplate(SmsProperties properties){
        this.properties = properties;
    }
    //获取手机和验证码发送短信
    public void sendSms(String phone,String code){
        //设置accessKey
        String accessKeyId = properties.getAccessKey();
        //设置accessKeySecret
        String accessKeySecret = properties.getSecret();
        try {
            Config config = new Config()
                    .setAccessKeyId(accessKeyId)
                    .setAccessKeySecret(accessKeySecret);
            config.endpoint = "dysmsapi.aliyuncs.com";
            com.aliyun.dysmsapi20170525.Client client = new com.aliyun.dysmsapi20170525.Client(config);

            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers( phone) //目标手机号
                    .setSignName(properties.getSignName()) //签名名称
                    .setTemplateCode(properties.getTemplateCode()) //短信模板code
                    .setTemplateParam("{\"code\":\""+code+"\"}"); //模板中变量替换
            SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            SendSmsResponseBody body = sendSmsResponse.getBody();
            System.out.println(body);
        }catch (Exception e){
            System.out.println(e);
        }
    }

}